package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketCollect;
import com.cskaoyan.market.db.domain.MarketCollectExample;
import com.cskaoyan.market.db.mapper.MarketCollectMapper;
import com.cskaoyan.market.service.MarketAdminCollectService;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class MarketAdminCollectServiceImpl implements MarketAdminCollectService {
    @Override
    public List<MarketCollect> list(String page, String limit, String userId, String valueId, String sort, String order) {
        // 获取session和example
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketCollectMapper mapper = sqlSession.getMapper(MarketCollectMapper.class);
        MarketCollectExample example = new MarketCollectExample();

        MarketCollectExample.Criteria criteria = example.or();

        if (!StringUtils.isEmpty(valueId)) {
            criteria.andValueIdEqualTo(Integer.parseInt(valueId));
        }
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        example.setOrderByClause(sort + " " + order);

        // 设置PageHelper,并执行查询
        int pageNum = Integer.parseInt(page);
        int pageSize = Integer.parseInt(limit);
        PageHelper.startPage(pageNum, pageSize);
        List<MarketCollect> collects = mapper.selectByExample(example);

        // 提交并关闭sqlSession
        sqlSession.commit();
        sqlSession.close();

        return  collects;

    }
}
